#ifndef __CLOUD_HOT_MANAGER__
#define __CLOUD_HOT_MANAGER__

#include "conf.hpp"
#include "backup_info.hpp"

namespace cloud
{

#define HOT_MANAGER_LOGGER "hot_manager_logger"
    class HotManager
    {
    private:
        int _hot_time;
        std::string _backup_path;
        std::string _compress_path;

    private:
        bool IsHot(const std::string& filename)
        {
            FileUtil fu(filename);
            time_t last_atime = fu.LastATime();
            time_t cur_time = time(NULL);
            if(cur_time - last_atime > _hot_time)
            {
                return false;
            }
            return true;
        }



    public:
        HotManager()
        {
            Configuration* conf = Configuration::GetInstance();
            _hot_time = conf->GetHotTime();
            _backup_path = conf->GetBackupDir();
            _compress_path = conf->GetCompressDir();
            
            std::unique_ptr<log_system::LoggerBuilder> glBuilder(new log_system::GlobalLoggerBuilder());
            glBuilder->buildLoggerName(HOT_MANAGER_LOGGER);
            glBuilder->buildLoggerType(log_system::LoggerType::LOGGER_ASYNC);
            glBuilder->buildSink<log_system::FileSink>("./log/hot_manager_logger.txt");

            log_system::Logger::ptr logger = glBuilder->build();
            //创建备份与压缩目录，避免后续操作中目录不存在
            if(FileUtil(_backup_path).CreateDir())  
                logger->warnLog("备份目录不存在,创建目录%s",_backup_path.c_str());
            if(FileUtil(_compress_path).CreateDir())    
                logger->warnLog("压缩目录不存在,创建目录%s",_compress_path.c_str());
        }

        void UncompressBeforeDownload(BackupInfo& info)
        {
            if(!info._is_hot)
            {
                FileUtil fu(info._compress_path);
                fu.Uncompress(info._backup_path);
                fu.Delete();
                info._is_hot = true;
                BackupInfoManager::GetInstance()->Update(info);
                log_system::getLogger(HOT_MANAGER_LOGGER)->infoLog("恢复%s为热点文件",FileUtil(info._backup_path).FileName().c_str());
            }
        }

        void CompressDetect()
        {
            Configuration* conf = Configuration::GetInstance();
            BackupInfoManager* infoManager = BackupInfoManager::GetInstance();

            log_system::Logger::ptr logger = log_system::getLogger(HOT_MANAGER_LOGGER);

            FileUtil fu(_backup_path);
            std::vector<std::string> filenames;
            fu.RetrieveFilenames(&filenames);

            for(auto filename : filenames)
            {
                if(!IsHot(filename))
                {
                    BackupInfo info;

                    std::string download_url = conf->GetDownloadPrefix() + FileUtil(filename).FileName();
                    
                    
                    //备份文件存在，但未记录
                    if(!infoManager->GetOneByUrl(download_url, &info))
                    {
                        //创建记录
                        BackupInfo::CreateNewBackupInfo(filename, &info);
                        logger->warnLog("存在未记录的备份文件%s,创建其备份信息", FileUtil(info._backup_path).FileName().c_str());
                    }

                    FileUtil fu(filename);
                    fu.Compress(info._compress_path);
                    fu.Delete();
                    info._is_hot = false;
                    infoManager->Update(info);
                    logger->infoLog("%s转为非热点文件", FileUtil(info._backup_path).FileName().c_str());
                }
            }
        }

        void RunCompressDetect()
        {
            log_system::Logger::ptr logger = log_system::getLogger(HOT_MANAGER_LOGGER);
            logger->infoLog("热点管理模块启动");
            while(true)
            {
                CompressDetect();
                //避免消耗过多CPU性能，1s一次检测
                std::this_thread::sleep_for(std::chrono::seconds(1));
            }
        }
    };
}



#endif